package archforce.atp.x.ui.impl.left;

import archforce.atp.x.ui.frame.component.AbstractRowItemComponent;
import archforce.atp.x.ui.impl.common.component.IPPortItemComponent;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wx
 * @version 1.0
 * @date 2022/4/8 11:33
 */
public class IPPortGroupComponent extends AbstractRowItemComponent<List<IPPortModel>> {
    private List<IPPortItemComponent> inputs;

    private static final Integer DEFAULT_INPUT_GROUP_COUNT = 3;

    public IPPortGroupComponent() {
        List<IPPortModel> defaultData = new ArrayList<>(DEFAULT_INPUT_GROUP_COUNT);
        for (int i = 0; i < DEFAULT_INPUT_GROUP_COUNT; i++) {
            defaultData.add(new IPPortModel(true));
        }
        init(new ArrayList<>((defaultData)));
    }

    public IPPortGroupComponent(List<IPPortModel> date) {
        init(date);
    }

    private void init(List<IPPortModel> date){
        this.inputs = new ArrayList<>(date.size());
        date.forEach(peer ->{
            IPPortItemComponent itemComponent = new IPPortItemComponent(peer);
            inputs.add(itemComponent);
            add(itemComponent);
        });
    }

    public List<IPPortItemComponent> getInputs() {
        return inputs;
    }

    @Override
    public void setBindData(List<IPPortModel> bindData) {
        if(bindData == null){

        }else{
            for (int i = 0; i < bindData.size(); i++) {
                this.inputs.get(i).setBindData(bindData.get(i));
            }
        }
    }

    @Override
    public List<IPPortModel> getBindData() {
        return this.inputs.stream().map(IPPortItemComponent::getBindData).collect(Collectors.toList());
    }
}
